#!/bin/bash
set -e
set -x
MYDIR="${0%/*}"
cd "$MYDIR/.."
source "recipes/common.sh"


# RECIPE START rpm-build
#
# depends: beecrypt
# depends: elfutils
# depends: gnu-config
# depends: gnulib
# depends: libdb4.7
# depends: popt

rm -rf build/rpm-build
mkdir -p build
### XXX: fails to build out of the source tree
cp -a rpm-build build/
reset_mtime build/rpm-build build/rpm-build/configure.ac
cd build/rpm-build
# XXX: bindir, sbindir, datadir, infodir, etc, must be set
# explicitely to get platform macros right
env \
	rpm_cv_glob_ext=yes \
	ac_cv_valistisarray=true \
	ac_cv_path___CPIO=/bin/cpio \
	ac_cv_path___UNZIP=/usr/bin/unzip \
	ac_cv_path___LZMA=/usr/bin/lzma \
	ac_cv_path___XZ=/usr/bin/xz \
	ac_cv_path___GPG=/usr/bin/gpg \
	ac_cv_path___SSH=/usr/bin/ssh \
	AS="${TARGET}-as" \
	LIBBEECRYPT='-lbeecrypt' \
./configure \
	${CONFIGURE_ARGS} \
	--exec-prefix=$prefix \
	--bindir=$prefix/bin \
	--sbindir=$prefix/sbin \
	--libexecdir=$prefix/libexec \
	--datadir=$prefix/share \
	--includedir=$prefix/include \
	--infodir=$prefix/share/info \
	--mandir=$prefix/share/man \
	--disable-static \
	--without-api-docs \
	--without-python \
	--without-selinux \
	--without-db \
	--program-transform-name= \
	${AUTOCRAP_KLUDGE}

# XXX: fails to build in parallel
make -j1
make -j1 install DESTDIR="`pwd`/inst"
wipe_la_files "`pwd`/inst"
mv inst${prefix}/lib/rpm/rpmrc inst${prefix}/lib/rpm/rpmbuildrc
mv inst${prefix}/lib/rpm/macros inst${prefix}/lib/rpm/buildmacros

# these should be provided by rpm
rm -f inst${prefix}/${target_libdir}/librpm.so
rm -f inst${prefix}/${target_libdir}/librpmbuild.so
rm -f inst${prefix}/${target_libdir}/librpmdb.so
rm -f inst${prefix}/${target_libdir}/librpmio.so

# loongarch64-linux -> loongarch64-alt-linux
find inst${prefix}/lib/rpm -maxdepth 1 -type d -name '*-alt-linux' | \
	sed -r -e  's;^(.*)/rpm/([^/]*)-alt-linux;\1/rpm/\2-alt-linux \1/rpm/\2-linux;' | \
	xargs -n2 ln -sfr

cd inst
cat > ${prefix#/}/lib/rpm/dummy.clean << EOF
#!/bin/true
EOF
chmod 755 ${prefix#/}/lib/rpm/dummy.clean
find ${prefix#/} | cpio -pumd "$stage"

cd ../../..
rm -rf build/rpm-build

cd rpm-build-perl
find ${prefix#/} | cpio -pumd "$stage"

# RECIPE END rpm-build
